import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:study_demo/ui/network/network_controller.dart';

class NetworkPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetX<NetworkController>(
        init: NetworkController(),
        builder: (controller) {
          return Scaffold(
            appBar: AppBar(title: const Text('网络状态监听')),
            body: Center(
                child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                if (controller.netState.value == ConnectivityResult.mobile)
                  const Icon(Icons.network_cell, color: Colors.green, size: 60),
                if (controller.netState.value == ConnectivityResult.wifi)
                  const Icon(Icons.wifi, color: Colors.blue, size: 60),
                if (controller.netState.value == ConnectivityResult.none)
                  const Icon(Icons.network_locked_outlined,
                      color: Colors.grey, size: 60),
                Padding(
                  padding: const EdgeInsets.only(top: 16),
                  child: Text('${controller.netState.value}'),
                ),
              ],
            )),
          );
        });
  }
}

class NetStatePanel extends StatelessWidget {
  final ConnectivityResult? state;

  const NetStatePanel({Key? key, required this.state}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        if (state == ConnectivityResult.mobile)
          const Icon(Icons.network_cell, color: Colors.green, size: 60),
        if (state == ConnectivityResult.wifi)
          const Icon(Icons.wifi, color: Colors.blue, size: 60),
        if (state == ConnectivityResult.none)
          const Icon(Icons.network_locked_outlined,
              color: Colors.grey, size: 60),
        Padding(
          padding: const EdgeInsets.only(top: 16),
          child: Text('$state'),
        ),
      ],
    );
  }
}
